package com.level2app.ui.login;

import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import com.level2app.R;
import com.level2app.base.BaseActivity;
import com.level2app.config.CoreManager;
import com.level2app.databinding.ActivityEmailLoginBinding;
import com.level2app.ui.login.view.EmailLoginViewModel;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.KeyboardUtils;
import com.level2app.utils.RegexUtils;

/**
 * 邮箱登录
 */
public class EmailLoginActivity extends BaseActivity<ActivityEmailLoginBinding, EmailLoginViewModel> {

    private int source = 0;//0--登录，1--注册

    @Override
    protected EmailLoginViewModel bindModel() {
        return getViewModel(this, EmailLoginViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar();
        binding.setEmailLoginViewModel(viewModel);
        source = getIntent().getIntExtra("source", 0);
        if (source == 0) {
            //登录
            binding.titleTv.setText(getString(R.string.login_easily_title));
            binding.contentTv.setText(getString(R.string.login_easily_content));
        } else {
            //注册
            binding.titleTv.setText(getString(R.string.sign_up_without_password));
            binding.contentTv.setText(getString(R.string.we_will_send));
        }
    }

    @Override
    protected void initClick() {
        super.initClick();
        viewModel.inputEmail.observe(this, s -> changeBtnStatus());
        viewModel.onDelayClick(binding.sendLinkTv, o -> {
            String email = viewModel.inputEmail.getValue();
            //校验电子邮件
            if (!RegexUtils.isEmail(email)){
                binding.errorLl.setVisibility(View.VISIBLE);
                return;
            }
            KeyboardUtils.hideSoftInput(binding.etEmail);
            CoreManager.setEmail(email);
            CoreManager.setIsLogin(source == 0);
            showLoadingDialog();
            if (source == 0) {
                viewModel.sendEmailLogin(viewModel.inputEmail.getValue());
            } else {
                //注册
                viewModel.sendEmailRegister(viewModel.inputEmail.getValue());
            }
        });
        viewModel.sendSuccessData.observe(this, messageBean -> {
            dismissLoadingDialog();
            Bundle bundle = new Bundle();
            bundle.putInt("source", source);
            bundle.putString("email", viewModel.inputEmail.getValue());
            JumpIntent.jump(this, CheckEmailActivity.class, bundle);
        });
        viewModel.sendFailedData.observe(this, messageBean -> {
            dismissLoadingDialog();
        });
    }

    private void changeBtnStatus() {
        if (viewModel.inputEmail.getValue() != null && viewModel.inputEmail.getValue().trim().length() > 0) {
            binding.bgLl.setBackgroundResource(R.drawable.shape_stroke_000000_99dp);
            binding.sendLinkTv.setBackgroundResource(R.drawable.shape_btn_bg);
            binding.sendLinkTv.setTextColor(Color.WHITE);
            binding.sendLinkTv.setEnabled(true);
            binding.emailIv.setImageResource(R.mipmap.icon_email);
        } else {
            binding.bgLl.setBackgroundResource(R.drawable.shape_stroke_e4e4e4_99dp);
            binding.sendLinkTv.setBackgroundResource(R.drawable.shape_btn_bg_def);
            binding.sendLinkTv.setTextColor(getColor(R.color.color_9ca3af));
            binding.sendLinkTv.setEnabled(false);
            binding.emailIv.setImageResource(R.mipmap.icon_email_def);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //返回具有焦点的当前视图
            View v = getCurrentFocus();
            if (KeyboardUtils.isShouldHideInput(v, ev)) {
                KeyboardUtils.hideSoftInput(this);
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}